package com.zyx.strings;

public class StringUtils {
    //获取上传文件名  "D:\\20120512\\day12\\Demo1.java"。
    public static String getFileName(String s){
        System.out.println("-----");
        //在java有一些字符被用了。你想表达真正得字符含义得，需要转移  \
        String[] strs = s.split("\\\\");
        return strs[strs.length-1];
    }

    //获取上传文件名  "D:\\20120512\\day12\\Demo1.java"。
    public static String getFileName2(String s){
        int i= s.lastIndexOf("\\");
        return s.substring(i+1);

    }

    public static String reverseString(String s){
        //1.把字符串转化成字符数组
        char[] chars = s.toCharArray();
        //['h',l,,l,e,o]
        //2.利用值交换，把数组元素倒过来
        for (int i = 0; i < chars.length/2; i++) {
            char temp =' ';
            temp = chars[i];
            chars[i] = chars[chars.length-i-1];
            chars[chars.length-i-1] = temp;
        }
        //将数组转换成字符串
        return String.valueOf(chars);
    }

    //4. 求一个子串在整串中出现的次数
		/*public static int getCount(String str,String substr){
			String[] splits = str.split(substr);
			for (int i = 0; i < splits.length; i++) {
				System.out.println(splits[i]);
			}
			return splits.length-1;

		}*/

    public static int getCount(String str,String substr){
        //  he1213he232hewwhe    he
        int count = 0;
        int index = 0;
        while(index!=-1){
            index = str.indexOf(substr);
            str= str.substring(index+1);
            if(index==-1){
                break;
            }
            count++;
        }

        return count;
    }
}
